<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者: 奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\controller;
class Template extends Base
{
	public function index(){
		$path = QL_ROOT."template/pc/";
		$dir = glob($path.'*/');

         $uninstall = [];
		 foreach($dir as $k=>$v){
            if(is_file($v.'config.xml')){
            	  $dirname = str_replace([$path,'/'],'',$v);
                  $xml = file_get_contents($v."config.xml");
                  $arr = xml_to_array($xml);
      			  $uninstall[$k] = xml_to_array($xml);
      			  $uninstall[$k]['dirname']= $dirname;  //目录名称

      			  $res = model('template')->getTemplateByName($arr['name']);
             
      			  $uninstall[$k]['status']=  $res['status'];
              $uninstall[$k]['thumb']=  $res['thumb'];
      			  $uninstall[$k]['id']=  $res['id'];



            }
		 }
   
        $this->assign('uninstall',$uninstall);
	    	return $this->fetch();
	}

	public function install(){
       $param = $this->request->param();
       if(!$param['dirname']){ error_json('参数错误！');}
       $path = QL_ROOT."template/pc/";
       if(is_file($path.$param['dirname'].'/config.xml')){
          $xml = file_get_contents($path.$param['dirname']."/config.xml"); 
          $arr = xml_to_array($xml);
          $data['name'] =$arr['name'];
          $data['dirname'] = $param['dirname'];
          $data['author']    = $arr['author'];
          $data['copyright'] = $arr['copyright'];
          $data['thumb'] = '/template/pc/'.$param['dirname'].'/cover.png';
          $data['add_time']  = time();
         
          if(model('template')->getTemplateByName($data['name'])){
          	return error_json('已经安装，请勿重复安装'); 
          }

            $res = model('template')->addData($data);
         
          return success_json('安装成功');
       }else{
       	 error_json('模板配置文件缺失');
       }
      
	}

	public function uninstall(){
          $param = $this->request->param(); 
          if(empty($param['id'])){
           return error_json('请求参数错误');
          }
          $where['id'] = intval($param['id']);
          model('template')->delData($where);
      
          return success_json('卸载成功');
 
      
	}


	public function open(){
       $param = $this->request->param();
          $data['status'] = 1;
          $where['id'] = $param['id'];
          model('template')->editData($data,$where);
          $where2 =  "id != ".$param['id'];
          $data2['status'] = 0;
          model('template')->editData($data2,$where2);

          $DefaultTheme = model('template')->getDefaultTheme();
          cache('template',$DefaultTheme); //生成PC端缓存
          return success_json('模板启用成功');
          
	}	


}